<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="属性名称" prop="propertyName">
        <el-select v-model="queryParams.propertyName" placeholder="请选择属性名称" clearable>
          <el-option
            v-for="dict in propertyNames"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      若要新增或修改请联系管理员
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="propertyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" prop="propertyId" />
      <el-table-column label="属性类型" align="center" :show-overflow-tooltip="true" >
        <template #default="scope">
          <router-link :to="'/apartment/data/index/'+scope.row.propertyId" class="link-type" v-if="scope.row.propertyType==='apartment'">
            <span>{{ scope.row.propertyType }}</span>
          </router-link>
          <router-link :to="'/apartment/roomdata/index/'+scope.row.propertyId" class="link-type" v-if="scope.row.propertyType==='room'">
            <span>{{ scope.row.propertyType }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="属性名称" align="center" prop="propertyName" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改属性管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="propertyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="属性类型" prop="propertyType">
          <el-input v-model="form.propertyType" placeholder="请输入属性类型" />
        </el-form-item>
        <el-form-item label="属性名称" prop="propertyName">
          <el-input v-model="form.propertyName" placeholder="请输入属性名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Property">
import { listProperty, getProperty, delProperty, addProperty, updateProperty } from "@/api/apartment/property";
import {values} from "vuedraggable/dist/vuedraggable.common.js";

const { proxy } = getCurrentInstance();
const { apartment_property_status, apartment_property_type } = proxy.useDict('apartment_property_status', 'apartment_property_type');

const propertyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    propertyType: null,
    propertyName: null,
    propertyStatus: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);
const propertyNames=[
    {value: 'apartment', label: '公寓属性'},
    {value: 'room', label: '房间属性'},
]
/** 查询属性管理列表 */
function getList() {
  loading.value = true;
  listProperty(queryParams.value).then(response => {
    propertyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    propertyId: null,
    propertyType: null,
    propertyName: null,
    propertyStatus: null
  };
  proxy.resetForm("propertyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.propertyId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加属性管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _propertyId = row.propertyId || ids.value
  getProperty(_propertyId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改属性管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["propertyRef"].validate(valid => {
    if (valid) {
      if (form.value.propertyId != null) {
        updateProperty(form.value).then(response => {
          proxy.$modal.msgSuccess("修改");
          open.value = false;
          getList();
        });
      } else {
        addProperty(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _propertyIds = row.propertyId || ids.value;
  proxy.$modal.confirm('是否确认删除属性管理编号为"' + _propertyIds + '"的数据项？').then(function() {
    return delProperty(_propertyIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('apartment/property/export', {
    ...queryParams.value
  }, `property_${new Date().getTime()}.xlsx`)
}

getList();
</script>
